{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1维: [1 2 3]\n",
      "多维: [[1 2]\n",
      " [3 4]]\n",
      "2维: [[1 2 3 4 5]]\n",
      "对角阵: [[1. 0. 0. 0.]\n",
      " [0. 1. 0. 0.]\n",
      " [0. 0. 1. 0.]\n",
      " [0. 0. 0. 1.]]\n",
      "dtype: [1.+0.j 2.+0.j 3.+0.j]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.array([1,2,3])  \n",
    "print('1维:',a)\n",
    "\n",
    "# 多于一个维度  \n",
    "a = np.array([[1,  2],  [3,  4]])  \n",
    "print('多维:',a)\n",
    "\n",
    "# 最小维度  \n",
    "import numpy as np \n",
    "a = np.array([1, 2, 3, 4, 5], ndmin =  2)  \n",
    "print('2维:',a)\n",
    "\n",
    "# 创建对角阵\n",
    "res = np.eye(4)\n",
    "print('对角阵:',res)\n",
    "\n",
    "# dtype 参数  \n",
    "a = np.array([1,  2,  3], dtype = complex)  \n",
    "print('dtype:',a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "int32\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "# 使用标量类型\n",
    "dt = np.dtype(np.int32)\n",
    "print(dt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "int32\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "# int8, int16, int32, int64 四种数据类型可以使用字符串 'i1', 'i2','i4','i8' 代替\n",
    "dt = np.dtype('i4')\n",
    "print(dt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "int32\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "# 字节顺序标注\n",
    "dt = np.dtype('<i4')\n",
    "print(dt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('age', 'i1')]\n"
     ]
    }
   ],
   "source": [
    "# 首先创建结构化数据类型\n",
    "import numpy as np\n",
    "dt = np.dtype([('age',np.int8)]) \n",
    "print(dt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(10,) (20,) (30,)]\n",
      "[10 20 30]\n"
     ]
    }
   ],
   "source": [
    "# 将数据类型应用于 ndarray 对象\n",
    "import numpy as np\n",
    "dt = np.dtype([('age',np.int8)]) \n",
    "a = np.array([(10,),(20,),(30,)], dtype = dt) \n",
    "print(a)\n",
    "\n",
    "# 类型字段名可以用于存取实际的 age 列\n",
    "print(a['age'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(b'abc', 21, 50.) (b'xyz', 18, 75.)]\n",
      "[b'abc' b'xyz']\n",
      "[21 18]\n",
      "[50. 75.]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "student = np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')]) \n",
    "a = np.array([('abc', 21, 50),('xyz', 18, 75)], dtype = student) \n",
    "print(a)\n",
    "print(a['name'])\n",
    "print(a['age'])\n",
    "print(a['marks'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]\n",
      "1\n",
      "[[[ 0  1  2]\n",
      "  [ 3  4  5]\n",
      "  [ 6  7  8]\n",
      "  [ 9 10 11]]\n",
      "\n",
      " [[12 13 14]\n",
      "  [15 16 17]\n",
      "  [18 19 20]\n",
      "  [21 22 23]]]\n",
      "3\n",
      "(2, 4, 3)\n",
      "[[[ 0  1  2]\n",
      "  [ 3  4  5]]\n",
      "\n",
      " [[ 6  7  8]\n",
      "  [ 9 10 11]]\n",
      "\n",
      " [[12 13 14]\n",
      "  [15 16 17]]\n",
      "\n",
      " [[18 19 20]\n",
      "  [21 22 23]]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np \n",
    " \n",
    "a = np.arange(24)  \n",
    "print(a)\n",
    "print (a.ndim)             # a 现只有一个维度\n",
    "# 现在调整其大小\n",
    "b = a.reshape(2,4,3)  # b 现在拥有三个维度\n",
    "print(b)\n",
    "print(b.ndim)\n",
    "print(b.shape)\n",
    "b.shape=(4,2,3)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "  C_CONTIGUOUS : True\n",
      "  F_CONTIGUOUS : True\n",
      "  OWNDATA : True\n",
      "  WRITEABLE : True\n",
      "  ALIGNED : True\n",
      "  WRITEBACKIFCOPY : False\n",
      "  UPDATEIFCOPY : False\n",
      "\n",
      "8\n",
      "  C_CONTIGUOUS : True\n",
      "  F_CONTIGUOUS : True\n",
      "  OWNDATA : True\n",
      "  WRITEABLE : True\n",
      "  ALIGNED : True\n",
      "  WRITEBACKIFCOPY : False\n",
      "  UPDATEIFCOPY : False\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import numpy as np \n",
    " \n",
    "# 数组的 dtype 为 int8（一个字节）  \n",
    "x = np.array([1,2,3,4,5], dtype = np.int8)  \n",
    "print(x.itemsize)\n",
    "print(x.flags)\n",
    " \n",
    "# 数组的 dtype 现在为 float64（八个字节） \n",
    "y = np.array([1,2,3,4,5], dtype = np.float64)  \n",
    "print(y.itemsize)\n",
    "print(y.flags)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0. 0. 0. 0. 0.]\n",
      "[0 0 0 0 0]\n",
      "[[(0, 0) (0, 0)]\n",
      " [(0, 0) (0, 0)]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    " \n",
    "# 默认为浮点数\n",
    "x = np.zeros(5) \n",
    "print(x)\n",
    " \n",
    "# 设置类型为整数\n",
    "y = np.zeros((5,), dtype = int) \n",
    "print(y)\n",
    " \n",
    "# 自定义类型\n",
    "z = np.zeros((2,2), dtype = [('x', 'i4'), ('y', 'i4')])  \n",
    "print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1. 1. 1. 1. 1.]\n",
      "[[1 1]\n",
      " [1 1]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    " \n",
    "# 默认为浮点数\n",
    "x = np.ones(5) \n",
    "print(x)\n",
    " \n",
    "# 自定义类型\n",
    "x = np.ones([2,2], dtype = int)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4607182418800017408                   0]\n",
      " [4611686018427387904                   0]\n",
      " [4613937818241073152                   0]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np \n",
    "x = np.empty([3,2], dtype = int) \n",
    "print (x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np \n",
    " \n",
    "x =  [1,2,3] \n",
    "a = np.asarray(x)  \n",
    "print (a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np \n",
    " \n",
    "x =  (1,2,3) \n",
    "a = np.asarray(x)  \n",
    "print (a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(1, 2, 3) (4, 5)]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np \n",
    " \n",
    "x =  [(1,2,3),(4,5)] \n",
    "a = np.asarray(x,dtype=object)  \n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1. 2. 3.]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np \n",
    " \n",
    "x =  [1,2,3] \n",
    "a = np.asarray(x, dtype =  float)  \n",
    "print (a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[b'H' b'e' b'l' b'l' b'o' b' ' b'W' b'o' b'r' b'l' b'd']\n"
     ]
    }
   ],
   "source": [
    "import numpy as np \n",
    " \n",
    "s =  b'Hello World' \n",
    "a = np.frombuffer(s, dtype =  'S1')  \n",
    "print (a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0. 1. 2. 3. 4.]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np \n",
    " \n",
    "# 使用 range 函数创建列表对象  \n",
    "list=range(5)\n",
    "it=iter(list)\n",
    " \n",
    "# 使用迭代器创建 ndarray \n",
    "x=np.fromiter(it, dtype=float)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    " \n",
    "x = np.arange(5)  \n",
    "print (x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0. 1. 2. 3. 4.]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    " \n",
    "# 设置了 dtype\n",
    "x = np.arange(5, dtype =  float)  \n",
    "print (x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10 12 14 16 18]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "x = np.arange(10,20,2)  \n",
    "print (x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10.]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.linspace(1,10,10)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.linspace(1,1,10)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10. 12. 14. 16. 18.]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    " \n",
    "a = np.linspace(10, 20,  5, endpoint =  False)  \n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(array([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.]), 1.0)\n",
      "[[ 1.]\n",
      " [ 2.]\n",
      " [ 3.]\n",
      " [ 4.]\n",
      " [ 5.]\n",
      " [ 6.]\n",
      " [ 7.]\n",
      " [ 8.]\n",
      " [ 9.]\n",
      " [10.]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a =np.linspace(1,10,10,retstep= True)\n",
    " \n",
    "print(a)\n",
    "# 拓展例子\n",
    "b =np.linspace(1,10,10).reshape([10,1])\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 10.          12.91549665  16.68100537  21.5443469   27.82559402\n",
      "  35.93813664  46.41588834  59.94842503  77.42636827 100.        ]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "# 默认底数是 10\n",
    "a = np.logspace(1.0,  2.0, num =  10)  \n",
    "print (a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[  1.   2.   4.   8.  16.  32.  64. 128. 256. 512.]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.logspace(0,9,10,base=2)\n",
    "print (a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [1 2 3]\n",
      " [1 2 3]\n",
      " [1 2 3]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np \n",
    " \n",
    "a = np.array([[ 0, 0, 0],\n",
    "           [10,10,10],\n",
    "           [20,20,20],\n",
    "           [30,30,30]])\n",
    "b = np.array([1,2,3])\n",
    "bb = np.tile(b, (4, 1))  # 重复 b 的各个维度\n",
    "print(bb)\n",
    "# print(a + bb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]]\n",
      "\n",
      "\n",
      "默认分割（0轴）：\n",
      "[array([[0, 1, 2, 3],\n",
      "       [4, 5, 6, 7]]), array([[ 8,  9, 10, 11],\n",
      "       [12, 13, 14, 15]])]\n",
      "\n",
      "\n",
      "分割（1轴）：\n",
      "[array([[ 0,  1],\n",
      "       [ 4,  5],\n",
      "       [ 8,  9],\n",
      "       [12, 13]]), array([[ 2,  3],\n",
      "       [ 6,  7],\n",
      "       [10, 11],\n",
      "       [14, 15]])]\n",
      "\n",
      "\n",
      "沿水平方向分割：\n",
      "[array([[ 0,  1],\n",
      "       [ 4,  5],\n",
      "       [ 8,  9],\n",
      "       [12, 13]]), array([[ 2,  3],\n",
      "       [ 6,  7],\n",
      "       [10, 11],\n",
      "       [14, 15]])]\n",
      "\n",
      "\n",
      "沿水平方向分割：\n",
      "[array([[ 0,  1],\n",
      "       [ 4,  5],\n",
      "       [ 8,  9],\n",
      "       [12, 13]]), array([[ 2,  3],\n",
      "       [ 6,  7],\n",
      "       [10, 11],\n",
      "       [14, 15]])]\n",
      "沿竖直方向分割：\n",
      "[array([[0, 1, 2, 3],\n",
      "       [4, 5, 6, 7]]), array([[ 8,  9, 10, 11],\n",
      "       [12, 13, 14, 15]])]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.arange(16).reshape(4, 4)\n",
    "print('第一个数组：')\n",
    "print(a)\n",
    "print('\\n')\n",
    "print('默认分割（0轴）：')\n",
    "b = np.split(a,2)\n",
    "print(b)\n",
    "print('\\n')\n",
    "\n",
    "print('分割（1轴）：')\n",
    "b = np.split(a,2,1)\n",
    "print(b)\n",
    "print('\\n')\n",
    "\n",
    "print('沿水平方向分割：')\n",
    "c = np.split(a,2,1)\n",
    "print(c)\n",
    "print('\\n')\n",
    "\n",
    "print('沿水平方向分割：')\n",
    "d= np.hsplit(a,2)\n",
    "print(d)\n",
    "\n",
    "print('沿竖直方向分割：')\n",
    "e= np.vsplit(a,2)\n",
    "print(e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "\n",
      "\n",
      "第一个数组的形状：\n",
      "(2, 3)\n",
      "\n",
      "\n",
      "第二个数组：\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "\n",
      "\n",
      "比较:\n",
      "[[1 2 3]\n",
      " [4 5 6]] \n",
      " [[1 2]\n",
      " [3 9]\n",
      " [5 6]]\n",
      "第二个数组的形状：\n",
      "(3, 2)\n",
      "\n",
      "\n",
      "修改第二个数组的大小：\n",
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [1 2 3]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    " \n",
    "a = np.array([[1,2,3],[4,5,6]])\n",
    " \n",
    "print ('第一个数组：')\n",
    "print (a)\n",
    "print ('\\n')\n",
    " \n",
    "print ('第一个数组的形状：')\n",
    "print (a.shape)\n",
    "print ('\\n')\n",
    "b = np.resize(a, (3,2))\n",
    " \n",
    "print ('第二个数组：')\n",
    "print (b)\n",
    "print ('\\n')\n",
    "\n",
    "print('比较:')\n",
    "b[1,1] = 9\n",
    "print(a,'\\n',b)\n",
    "\n",
    "print ('第二个数组的形状：')\n",
    "print (b.shape)\n",
    "print ('\\n')\n",
    "# 要注意 a 的第一行在 b 中重复出现，因为尺寸变大了\n",
    " \n",
    "print ('修改第二个数组的大小：')\n",
    "b = np.resize(a,(3,3))\n",
    "print (b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组 a：\n",
      "[[ 1  1  1]\n",
      " [ 0  2  5]\n",
      " [ 2  5 -1]]\n",
      "a 的逆：\n",
      "[[ 1.28571429 -0.28571429 -0.14285714]\n",
      " [-0.47619048  0.14285714  0.23809524]\n",
      " [ 0.19047619  0.14285714 -0.0952381 ]]\n",
      "矩阵 b：\n",
      "[[ 6]\n",
      " [-4]\n",
      " [27]]\n",
      "计算：A^(-1)B：\n",
      "[[ 5.]\n",
      " [ 3.]\n",
      " [-2.]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np \n",
    " \n",
    "a = np.array([[1,1,1],[0,2,5],[2,5,-1]]) \n",
    " \n",
    "print ('数组 a：')\n",
    "print (a)\n",
    "ainv = np.linalg.inv(a) \n",
    " \n",
    "print ('a 的逆：')\n",
    "print (ainv)\n",
    " \n",
    "print ('矩阵 b：')\n",
    "b = np.array([[6],[-4],[27]]) \n",
    "print (b)\n",
    " \n",
    "print ('计算：A^(-1)B：')\n",
    "x = np.linalg.solve(a,b) \n",
    "print (x)\n",
    "# 这就是线性方向 x = 5, y = 3, z = -2 的解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
